using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace Kaskelot.SP.UI.Features.Masterpages
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("f31774d8-dfe1-45c5-b575-b25434738b56")]
    public class MasterpagesEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //Logging.LogVerbose(this, Logging.LayerNames.UI, "MasterpagesEventReceiver - FeatureActivated", "Entered ->");
            try
            {
                SPWeb currentWeb = ((SPSite)properties.Feature.Parent).RootWeb;
                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(currentWeb);
                pubWeb.CustomMasterUrl.SetValue(currentWeb.ServerRelativeUrl.TrimEnd('/') + "/_catalogs/masterpage/KaskelotMain.master", true);
                //pubWeb.MasterUrl.SetValue(currentWeb.ServerRelativeUrl.TrimEnd('/') + "/_catalogs/masterpage/KaskelotSystem.master", true);
                pubWeb.Update();
            }
            catch (Exception ex)
            {
                //Logging.LogError(this, Logging.LayerNames.UI, "MasterpagesEventReceiver - FeatureActivated", "Error", ex);
                throw;
            }
            //Logging.LogVerbose(this, Logging.LayerNames.UI, "MasterpagesEventReceiver - FeatureActivated", "Exiting ->");
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
